package core;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class WriterThread implements Runnable{

	private OutputStream out;
	private List<String> queues;
	private long delayTime = 100;
	
	public WriterThread(OutputStream out) {
		this.out = out;
		queues = new ArrayList<String>();
		new Thread(this).start();
	}
	
	@Override
	public void run() {
		while (true){
			try {
				synchronized (queues) {
					if (queues.size()>0){
						String message = queues.remove(0);
						System.out.println(message);
						out.write(message.getBytes());
						Thread.sleep(delayTime);
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
	
	public synchronized void QueueMessage(String message){
		queues.add(message);
	}
}
